﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Krist_em_up.Characters;
using Krist_em_up.Characters.Enemies;

namespace Krist_em_up.Item.Projectiles
{
    class Grenade : Projectile
    {
        protected TimeSpan countDown,start,t;
        Vector2 init;


        public Grenade(int shooter, Kristemup k, Vector2 position, Vector2 direction)
            : base(shooter, k, position, direction)
        {
            texture = k.Content.Load<Texture2D>("Grenade");
            speed = new Vector2(15,15);
            init = position;
            lifeTime = new TimeSpan(0, 0, 0, 3, 0);
            countDown = lifeTime;
            start = k.time;
        }

        public override void Update(GameTime gameTime)
        {
            countDown -= gameTime.ElapsedGameTime;
            t = Game.time-start;
            //float temps = t.Tot
            //base.Update(gameTime);
            if (HandleCollision() || lifeTime <= TimeSpan.Zero)
            {
                Explosion();
                Game.bullets.Remove(this);
            }
            else
            {
                position.X += direction.X * speed.X ;
                position.Y += 5 + speed.Y * direction.Y;
                speed.Y--;
            }
        }
        public override void Draw(GameTime gameTime,Vector2 originG)
        {
            SpriteEffects effect = direction.X <= -1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
            spriteBatch.Begin();
            spriteBatch.Draw(texture, new Rectangle((int)(position.X - origin.X - originG.X), (int)(position.Y - origin.Y - originG.Y), texture.Width, texture.Height),
                null, Color.White, (float)t.TotalSeconds, origin, effect, 0f);            
            spriteBatch.End();
        }

        public void Explosion()
        {
            Rectangle souffle = new Rectangle((int)position.X - 50, (int)position.Y  - 50,
                 (int)100, (int)100);
            Enemy[] temp = new Enemy[Game.Enemies.Count];
            Game.Enemies.CopyTo(temp, 0);
            foreach (Enemy e in temp)
            {
                if (e.GetRec().Intersects(souffle))
                {
                    e.estTouche(Game.Enemies, shooter);
                }
            }
        }
    }
}
